package com.vhuomai.product.search.service.impl;

import com.vhuomai.product.entity.BaseProduct;
import com.vhuomai.product.search.dto.ProductSearch;
import com.vhuomai.product.search.repository.ProductSearchRepository;
import com.vhuomai.product.service.ProductService;
import com.vhuomai.product.support.ProductEvent;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author harris
 */
@Service
public class ProductSearchServiceImpl implements ProductEvent {
    
    @Autowired
    private ProductSearchRepository searchRepository;
    @Autowired
    private ProductService productService;
    
    @Override
    public void onUpdate(int productId) {
        BaseProduct product = productService.findSimpleById(productId);
        if (product == null) {
            return;
        }
        ProductSearch productSearch = new ProductSearch();
        BeanUtils.copyProperties(product, productSearch);
        searchRepository.save(productSearch);
    }
    
    @Override
    public void onDelete(int productId) {
        searchRepository.delete(productId);
    }
}
